package com.unknow.first.dapp.admin.account.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lianziyou.chain.common.api.CommonPage;
import com.unknow.first.dapp.admin.account.mapper.AddressAccountDetail;
import com.unknow.first.dapp.admin.account.mapper.AddressCoinConfig;
import com.unknow.first.dapp.admin.account.vo.ChargeVO;
import org.cloud.exception.BusinessException;
import org.cloud.utils.CollectionUtil;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author Administrator
 * @description 针对表【t_address_account_detail】的数据库操作Service
 * @createDate 2022-05-17 10:21:36
 */
public interface AddressAccountDetailService extends IService<AddressAccountDetail> {

    /**
     * 创建交易
     *
     * @throws Exception
     */
    public int createTrade(AddressAccountDetail accountDetail) throws Exception;

    /**
     * 创建交易
     *
     * @throws Exception
     */
//    public AddressAccountDetail createWithdraw(String address, Integer coinId,final BigDecimal originAmount) throws Exception;


    public CommonPage<AddressAccountDetail> findListByPage(Integer page, Integer limit, String orderBy,
        QueryWrapper<AddressAccountDetail> queryWrapper) throws Exception;

    CommonPage<AddressAccountDetail> findListByPageHistory(Integer page, Integer limit, String orderBy, Map<String, Object> queryParams) throws Exception;

    /**
     * 签名订单数据
     *
     * @param address           地址
     * @param amount            订单金额
     * @param addressCoinConfig 币种信息
     * @return 签名数据
     * @throws Exception 异常
     */
    public JSONObject signOrderData(String address, BigDecimal amount, AddressCoinConfig addressCoinConfig) throws Exception;

    /**
     * 提现审核
     *
     * @param addressAccountDetailId 资金记录ID
     * @param isPass                 是否通过
     * @param reason 原因
     * @return 更新数据的记录
     * @throws Exception 异常
     */
//    public int authWithdraw(Long addressAccountDetailId, @NotNull Boolean isPass,String reason) throws Exception;

    default int updateAccountDetailStatus(Long addressAccountDetailId, final String userName, int tradeStatus, String message)
        throws Exception {

        AddressAccountDetail addressAccountDetail = this.getBaseMapper().selectById(addressAccountDetailId);

        JSONObject jsonObject = new JSONObject();
        if (CollectionUtil.single().isNotEmpty(addressAccountDetail.getRemark())) {
            jsonObject = JSONObject.parseObject(addressAccountDetail.getRemark());
        }
        jsonObject.put("errorMsg", message);

        QueryWrapper<AddressAccountDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_account_detail_id", addressAccountDetailId);
        queryWrapper.eq("status", addressAccountDetail.getStatus());

        AddressAccountDetail addressAccountDetailUpdate = new AddressAccountDetail();
        addressAccountDetailUpdate.setAddressAccountDetailId(addressAccountDetailId);
        addressAccountDetailUpdate.setStatus(tradeStatus);
        addressAccountDetailUpdate.setUpdatedBy(userName);
        return this.getBaseMapper().update(addressAccountDetailUpdate, queryWrapper);
    }

    void checkChargeAmount(ChargeVO chargeAmountVO, String address) throws BusinessException;

    /**
     * 自动处理那些遗漏事件的已完成提现订单
     */
//    void processWithdrawOrderStatusSuccess() throws IOException;


}
